$(document).ready(function(){
	
	var erroMsg = $("#msg").html();
	if(typeof(erroMsg) != "undefined" && erroMsg.length > 0) {
		$(".loginError").html("账号或密码错误.");
		$(".userNameLi").css({"margin-top":"30px"});
	}
	
	//设置标题
	$(".headerTitle").text("用户登录");
	
	//初始化文本提示信息
	var initLoginNameText = "帐号";
	
	//监听键盘事件
	 $('.userName').bind("keydown",function(e){
		 var $this = $(this);
		 var keyCode = e.keyCode || e.which;
		 if($this.val() && $this.val().length == 1 && keyCode == 8 || $.trim($this.val()) == initLoginNameText){
				$(".clean").css({display: "none"});
		}else{
			$(".clean").css({display: "block"});
		}
	});
	
	//点击事件
	$(document).click(function(){
		if($(".userName").val().length > 0 && $.trim($(".userName").val()) != initLoginNameText){
			$(".clean").css({display: "block"});
		}else{
			$(".clean").css({display: "none"});
		}
	});
	
	$('.userName').focus(function(){
		 var $this = $(this);
		if($.trim($this.val()) == initLoginNameText){
			$this.val("");
		}
	});
	
	$('.userName').blur(function(){
		 var $this = $(this);
		if(!$.trim($this.val())){
			$this.val(initLoginNameText);
		}
	});
	
	$('.password').focus(function(){
		 var $this = $(this);
		 if($.trim($(".password").val()).length <= 0 && $(".pwdText").css("display") == "block"){
			$(".pwdText").css({display: "none"});
		}
	});
	
	$('.password').blur(function(){
		 var $this = $(this);
		 if($.trim($(".password").val()).length <= 0 && $(".pwdText").css("display") == "none"){
			$(".pwdText").css({display: "block"});
		 }
	});
	
	$(".pwdText").live("click",function(){
		$('.password').focus();
	});
	
	$('.codeText').live("focus",function(){
		 var $this = $(this);
		 $(".codeText").val("");
		if($.trim($this.val()) == initCodeText){
			$this.val("");
		}
	});
	
	$('.codeText').live("blur",function(){
		 var $this = $(this);
		if(!$.trim($this.val())){
			$this.val(initCodeText);
		}
	});
	
	//清除帐号文本框
	$(".clean").live("click",function(e){
		var $this = $(this);
		$(".userName").val("");
		$(".clean").css({display: ""});
	});
	
	//登录操作
	$(".login").on("click",function(){
		var $this = $(this);
		$this.attr("disabled","disabled");
		$this.text("正在登录");
		var loginName = $.trim($(".userName").val());
		var password = $.trim($(".password").val());
		if(loginName == null || loginName.length <= 0 || loginName == initLoginNameText || password == null || password.length <= 0){
			$this.text("登录");
			$this.removeAttr("disabled");
			$(".loginError").text("请输入帐号和密码");
			$(".userNameLi").css({"margin-top":"30px"});
			$(".userName").focus();
			return;
		}
		$("#fm1").submit();
	});
	
	var form = $(".form");
	
	// 回车事件
	form.on("keypress", "input", function(event){
		if(event.keyCode == 13){
			$(".login").trigger("click");
			return false;
		}
	})
	
	//初始化定义光标位置
	$(".password").focus();
});

